package com.ruoyi.system.mq;

import com.ruoyi.system.domain.notificationResult.MonthNotification;
import com.ruoyi.system.domain.notificationResult.OrderNotification;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * createMonthPConsumer
 * 月卡过期消息 --- 生产者
 * @author life.
 * @date 2025/4/5
 */
@Component
@RocketMQMessageListener(topic = "create-month-pass", consumerGroup = "create-month-consumer")
public class createMonthPConsumer implements RocketMQListener<List<MonthNotification>> {
        private static final Logger logger = LoggerFactory.getLogger(createMonthPConsumer.class);

        @Autowired
        private RocketMQTemplate rocketMQTemplate;

        @Override
        public void onMessage(List<MonthNotification> monthNotifications) {
            try {
                /*
                 * 缴费消息生产者
                 * */
                GenericMessage<List<MonthNotification>> listGenericMessage = new GenericMessage<>(monthNotifications);

                // 发送消息，并添加发送结果回调
                rocketMQTemplate.asyncSend("cancel-month-pass", listGenericMessage, new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        logger.info("消息发送成功，消息内容: {}, 发送结果: {}", listGenericMessage, sendResult);
                    }

                    @Override
                    public void onException(Throwable e) {
                        logger.error("消息发送失败，消息内容: {}", listGenericMessage, e);
                    }
                });
                logger.info("接收到的消息: {}", listGenericMessage);
            } catch (Exception e) {

            }
        }

}